package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BusinessCase;

import DAO.SessionDAO;

/**
 * Servlet implementation class HomeServlet
 */
public class HomeServlet extends BaseServlet  
{
	private static final long serialVersionUID = 1L;
	private final String MAIN_PAGE = "/mainPage.jsp";
	
	protected void loadData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		BusinessCase bc = SessionDAO.getInstance().getCachedBC();
		
		if(bc!= null)
			request.setAttribute("BC", bc);
		
		request.getRequestDispatcher(MAIN_PAGE).forward(request, response);
		System.out.println("has been directed to: " + MAIN_PAGE);
	}
	
	
	protected void saveMainData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String bcName = request.getParameter("bcName");
		
		if(!bcName.trim().equals(""))
		{
			BusinessCase bc = SessionDAO.getInstance().getCachedBC();
			
			if(bc == null)
			{
				bc = new BusinessCase();
				SessionDAO.getInstance().setCachedBC(bc);
			}
			
			bc.setName(bcName);
			request.setAttribute("BC", bc);
		}
		//else
			//request.setAttribute("validationError", "Business Case Name");
		
		request.getRequestDispatcher(MAIN_PAGE).forward(request, response);
	}
}
